package com.oa.system.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oa.entity.ExtMsgResult;
import com.oa.system.entity.UserAccount;
import com.oa.system.entity.UserInfo;
import com.oa.system.service.SystemService;
import com.oa.user.dto.UserInfoDto;

@Controller
@RequestMapping("/system")
public class SystemController {
	@Resource
	private SystemService systemService;

	// system/register 注册
	@PostMapping("/register")
	public @ResponseBody ExtMsgResult register(UserAccount userAccount, UserInfo userInfo) {
		try {
			systemService.register(userAccount, userInfo);
			return new ExtMsgResult(200, "注册成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtMsgResult(400, "注册失败");
		}
	}

	// system/login 登录系统
	@PostMapping("/login")
	public @ResponseBody ExtMsgResult login(UserAccount userAccount, boolean rememberMe) {
		UserInfoDto userInfo = systemService.login(userAccount, rememberMe);
		if (userInfo != null) {
			return new ExtMsgResult(200, "登录成功");
		} else {
			return new ExtMsgResult(400, "登录失败");
		}
	}

	// system/checkMe 自动登录
	@PostMapping("/checkMe")
	public @ResponseBody ExtMsgResult checkMe() {
		if (systemService.checkRememberMe()) {
			return new ExtMsgResult(200, "通过Remember Me登录");
		} else {
			return new ExtMsgResult(400, "没有Remember Me信息");
		}
	}

	// system/logoutAction 登出系统
	@PostMapping("/logoutAction")
	public @ResponseBody ExtMsgResult logout() {
		try {
			systemService.logout();
			return new ExtMsgResult(200, "登出成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtMsgResult(400, "登出失败!");
		}
	}

	@GetMapping("/login")
	public @ResponseBody ExtMsgResult login() {
		return new ExtMsgResult(200, "进入登录页");
	}
}
